package com.mti.app.view
{
	import com.mti.app.ApplicationFacade;
	import com.mti.app.view.mycomponents.EvenementView;
	import com.mti.epivisite.common.event.EpivisiteEvent;
	import com.mti.epivisite.common.event.EventProxyBase;
	import com.mti.epivisite.common.event.WaypointView;
	
	import flash.events.MouseEvent;
	
	import media.MediaPoint;
	
	import mx.collections.ArrayCollection;
	import mx.events.CollectionEvent;
	import mx.graphics.SolidColorStroke;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import spark.primitives.Line;
	
	public class EvenementViewMediator extends Mediator
	{
		public static const NAME:String = "EvenementViewMediator";
		
		[Bindable]
		public var eventProxy:EventProxyBase = null;
		
		public function EvenementViewMediator(viewComponent:EvenementView)
		{
			super(NAME, viewComponent);
			
			eventProxy = (facade.retrieveMediator(ApplicationMediator.NAME) as ApplicationMediator).serverEvent;
			
			view.mediator = this;
			view.routeButton.addEventListener(MouseEvent.CLICK, onCreateRouteClick);
		}
		
		private function get modifiedEvent():EpivisiteEvent
		{
			return eventProxy.modifiedEvent;
		}
		
		protected function get view():EvenementView
		{
			return viewComponent as EvenementView;
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				ApplicationFacade.LOADING_EVENEMENT_COMPLETE,
				EventProxyBase.GET_POINTS_SUCCESS
			];
		}
		
		override public function handleNotification(note:INotification):void
		{
			switch (note.getName())
			{
				case ApplicationFacade.LOADING_EVENEMENT_COMPLETE:
					this.view.source = this.view.fieldImage.source;

					for (var i:int = 1; i < view.mapContainer.numChildren; i++) 
					{
						view.mapContainer.removeElementAt(i);
					}

					if (modifiedEvent.pointList != null)
						displayPoints();

					break;
				
				case EventProxyBase.GET_POINTS_SUCCESS:
					displayPoints();
					eventProxy.modifiedEvent.pointList.addEventListener(CollectionEvent.COLLECTION_CHANGE, pointListCollectionChangeHandler);
					break;
			}
		}
		
		private function pointListCollectionChangeHandler(event:CollectionEvent):void
		{
			displayPoints();
		}
		
		public var route:ArrayCollection = null;
		private var lineCollection:ArrayCollection = null;
		private var isCreatingRoute:Boolean = false;
		
		private function onCreateRouteClick(event:MouseEvent):void
		{
			if (isCreatingRoute == false)
			{
				view.currentState = EvenementView.STATE_CREATING_ROUTE;
				route = new ArrayCollection();
				if (lineCollection != null)
				{
					for each (var l:Line in lineCollection)
					{
						view.mapContainer.removeElement(l);
					}
				}
				lineCollection = new ArrayCollection();
				view.mapContainer.addEventListener(MouseEvent.CLICK, onMapClick);
				isCreatingRoute = true;
			}
			else
			{
				stopCreatingRoute();
			}
		}
		
		public function stopCreatingRoute():void
		{
			if (isCreatingRoute == true)
			{
				view.mapContainer.removeEventListener(MouseEvent.CLICK, onMapClick);
				view.currentState = EvenementView.STATE_NORMAL;
				isCreatingRoute = false;
			}
		}
		
		public function drawRoute():void
		{
			if (lineCollection != null)
			{
				for each (var l:Line in lineCollection)
				{
					if (view.mapContainer.containsElement(l))
						view.mapContainer.removeElement(l);
				}
			}
			lineCollection = new ArrayCollection();
			
			for (var i:int = 1; i < route.length; ++i)
			{
				var wp:MediaPoint = MediaPoint(route.getItemAt(i));
				var oldWp:MediaPoint = MediaPoint(route.getItemAt(i - 1));
				var line:Line = new Line();
				line.xFrom = oldWp.posX;
				line.xTo = wp.posX;
				line.yFrom = oldWp.posY;
				line.yTo = wp.posY;
				line.stroke = new SolidColorStroke(0xff0000);
				lineCollection.addItem(line);
				view.mapContainer.addElement(line);
			}
		}
		
		private function onMapClick(event:MouseEvent):void
		{
			 var wp:MediaPoint = new MediaPoint();
			 wp.posX = view.mapContainer.mouseX;
			 wp.posY = view.mapContainer.mouseY;
			 wp.name = "wp";
			 wp.title = wp.name;
			 wp.shortDescription = "";
			 wp.description = "";
			 wp.Visited = true;
			 
			 route.addItem(wp);
			 
			 if (route.length > 1)
			 {
				 var oldWp:MediaPoint = MediaPoint(route.getItemAt(route.length - 2));
				 var line:Line = new Line();
				 line.xFrom = oldWp.posX;
				 line.xTo = wp.posX;
				 line.yFrom = oldWp.posY;
				 line.yTo = wp.posY;
				 line.stroke = new SolidColorStroke(0xff0000);
				 lineCollection.addItem(line);
				 view.mapContainer.addElement(line);
			 }
		}
		
		private var waypointViewList:ArrayCollection = new ArrayCollection();
		
		private function displayPoints():void
		{
			var wpView:WaypointView = null;
			for each (wpView in waypointViewList)
			{
				view.mapContainer.removeElement(wpView);
			}
			
			for each (var point:MediaPoint in modifiedEvent.pointList) 
			{
				wpView = new WaypointView();
				wpView.waypoint = point;
				view.mapContainer.addElement(wpView);
			}
		}
	}
}